/*
 * Created by JFormDesigner on Thu Jun 08 20:46:09 CST 2023
 */

package gui.admin;

import dao.PurchaseDao;
import dao.PurchaseDetailsDao;
import dao.PurchaseDao;
import dao.SaleDetailsDao;
import entity.Purchase;
import entity.PurchaseDetails;
import entity.Sale;
import entity.SaleDetails;
import gui.admin.tablemodel.PurchaseDetailsTableModel;
import gui.admin.tablemodel.PurchaseTableModel;
import gui.admin.tablemodel.SaleTableModel;
import gui.user.model.SaleDetailsTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class ManagePurchaseFrame extends JFrame {
    public ManagePurchaseFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        scrollPane2 = new JScrollPane();
        table2 = new JTable();
        label2 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setTitle("\u4f9b\u8d27\u5355\u7ba1\u7406\u9875\u9762");
        setPreferredSize(new Dimension(1350, 645));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
            table1.setModel(new PurchaseTableModel());
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(45, 75, 600, 400);

        //给表格添加鼠标点击事件
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                String purchaseId = (String) table1.getValueAt(row,0);//获取销售单编号
                try {
                    table2.setModel(new PurchaseDetailsTableModel(purchaseId));
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        //---- label1 ----
        label1.setText("\u4f9b\u8d27\u5355");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(320, 20, 120, 40);

        //======== scrollPane2 ========
        {
            scrollPane2.setViewportView(table2);
        }
        contentPane.add(scrollPane2);
        scrollPane2.setBounds(695, 75, 600, 400);

        //---- label2 ----
        label2.setText("\u4f9b\u8d27\u5355\u8be6\u60c5");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(955, 20, 120, 40);

        //---- button1 ----
        button1.setText("\u5220\u9664");
        button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button1);
        button1.setBounds(540, 505, 105, 50);
        //给添加按钮添加交互事件
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                String purchaseId = (String) table1.getValueAt(row,0);//获取销售单编号

                PurchaseDao purchaseDao = new PurchaseDao();
                try {
                    //获取指定供货单信息
                    Purchase purchase = purchaseDao.getAppointPurchase2(purchaseId);
                    PurchaseDetailsDao purchaseDetailsDao = new PurchaseDetailsDao();
                    try {
                        //获取指定供货单下的所有供货单明细信息
                        List purchaseDetailsList = purchaseDetailsDao.getAppointPurchaseDeatils(purchaseId);
                        //遍历查询列表，依次删除没个查询到的供货单明细
                        for (int i = 0; i < purchaseDetailsList.size(); i++) {
                            PurchaseDetails purchaseDetails = (PurchaseDetails) purchaseDetailsList.get(i);
                            purchaseDetailsDao.deletePurchaseDetails(purchaseDetails);//调用删除方法
                        }
                        purchaseDao.deletePurchase(purchase);//删除供货单
                        table1.setModel(new PurchaseTableModel());//刷新表格
                        JOptionPane.showMessageDialog(null,"删除成功！");
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //---- button2 ----
        button2.setText("\u8fd4\u56de");
        button2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button2);
        button2.setBounds(695, 505, 105, 50);

        //给取消按钮添加交互事件
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();//关闭当前窗口
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JScrollPane scrollPane2;
    private JTable table2;
    private JLabel label2;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        ManagePurchaseFrame managePurchaseFrame = new ManagePurchaseFrame();
    }
}
